package com.frxuexi.park2302.notice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.http.WebSocketClient
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2302.R
import com.frxuexi.park2302.databinding.ActivityNoticeAddBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NoticeAddActivity")
@AndroidEntryPoint
class NoticeAddActivity : BaseActivity<ActivityNoticeAddBinding, NoticeIntent, NoticeViewModel>() {
    override val binding: ActivityNoticeAddBinding by lazy {
        ActivityNoticeAddBinding.inflate(
            layoutInflater
        )
    }
    override val vm: NoticeViewModel by viewModels<NoticeViewModel>()
    lateinit var webClient: WebSocketClient
    override fun initData() {
        webClient.myData.observe(this) {
            ToastUtils.showLong(it)
        }
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        finish()
                    }

                    else -> {}
                }

            }
        }
    }

    override fun initView() {
        binding.btnNotice.setEvent("发布公告")
        webClient = WebSocketClient("ws://10.161.9.80:7015/api/websocket/100")
        webClient.start()
        binding.btnNotice.setOnClickListener {
            val title = binding.noticeaddTitle.text.toString()
            webClient.sendMessage(title)
            val item = NoticeBeanItem(
                binding.noticeaddContent.text.toString(),
                null,
                vm.getDataTime(System.currentTimeMillis()),
                "",
                title,
                "",
                SPUtils.getInstance().getInt(Const.UID)
            )
            vm.sendIntent(NoticeIntent.addNotice(item))
        }
    }

}